﻿using Md.Api.Entity.ModelDB;
using PetaPoco.NetCore;

namespace Md.Petapoco.Repositories.MdTrainingEducation;
/// <summary>
/// 轮播图仓储
/// </summary>
public class MdBannerRep : BaseRep<MdBannerModel>
{
    /// <summary>
    /// 通过编号获取banner信息
    /// </summary>
    /// <param name="id">banner编号</param>
    /// <returns>MdBannerModel</returns>
    public MdBannerModel GetMdBannerById(long? id)
    {
        #region ===sql===
        string sqlStr = string.Format(@"
            select `id`
                  ,`banner_title`
                  ,`banner_image`
                  ,`banner_url`
                  ,`banner_type`
                  ,`sort`
                  ,`remark`
                  ,`status`
                  ,`del_flag`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
            from `md_banner`
            where 1 = 1 and `id` = @0
        ");
        #endregion

        return MdTrainingEducationDB.GetInstance().SingleOrDefault<MdBannerModel>(sqlStr, id);
    }
    /// <summary>
    /// 获取banner列表数据
    /// </summary>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="model">筛选条件</param>
    /// <returns>IEnumerable<MdBannerModel></returns>
    public IEnumerable<MdBannerModel> GetMdBannerList(string? orderBy, bool? isDesc, MdBannerModel model)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.Id > 0)
        {
            sqlWhere += " and id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.BannerTitle))
        {
            sqlWhere += " and banner_title like @1 ";
        }
        if (model.BannerType > 0)
        {
            sqlWhere += " and banner_type = @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DelFlag))
        {
            sqlWhere += " and del_flag = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @6 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select `id`
                  ,`banner_title`
                  ,`banner_image`
                  ,`banner_url`
                  ,`banner_type`
                  ,`sort`
                  ,`remark`
                  ,`status`
                  ,`del_flag`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
            from `md_banner`
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Query<MdBannerModel>(
            sqlStr,
            model.Id,
            string.Format(@"%{0}%", model.BannerTitle),
            model.BannerType,
            model.Status,
            model.DelFlag,
            model.CreateBy,
            model.UpdateBy);
    }
    /// <summary>
    /// 分页获取banner列表数据
    /// </summary>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="model">筛选条件</param>
    /// <returns>Page<MdBannerModel></returns>
    public Page<MdBannerModel> GetMdBannerPageList(string? orderBy, bool? isDesc, int pageIndex, int pageSize, MdBannerModel model)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.Id > 0)
        {
            sqlWhere += " and id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.BannerTitle))
        {
            sqlWhere += " and banner_title like @1 ";
        }
        if (model.BannerType > 0)
        {
            sqlWhere += " and banner_type = @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DelFlag))
        {
            sqlWhere += " and del_flag = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @6 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select `id`
                  ,`banner_title`
                  ,`banner_image`
                  ,`banner_url`
                  ,`banner_type`
                  ,`sort`
                  ,`remark`
                  ,`status`
                  ,`del_flag`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
            from `md_banner`
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Page<MdBannerModel>(
            pageIndex,
            pageSize,
            sqlStr,
            model.Id,
            string.Format(@"%{0}%", model.BannerTitle),
            model.BannerType,
            model.Status,
            model.DelFlag,
            model.CreateBy,
            model.UpdateBy);
    }
    /// <summary>
    /// 通过编号更新banner
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateMdBannerById(MdBannerModel model)
    {
        var setList = new List<string>();

        #region ===set===
        setList.Add(" banner_title = @2 ");

        setList.Add(" banner_image = @3 ");

        setList.Add(" banner_url = @4 ");

        setList.Add(" banner_type = @5 ");

        setList.Add(" sort = @6 ");

        setList.Add(" status = @7 ");

        setList.Add(" update_by = @8 ");

        setList.Add(" update_time = @9 ");

        setList.Add(" remark = @10 ");
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
                set {0}
                where 1 = 1 and del_flag = @0 and id = @1
         ", string.Join(",", setList));
        #endregion

        var res = MdTrainingEducationDB.GetInstance().Update<MdBannerModel>(
            sqlStr,
            model.DelFlag,
            model.Id,
            model.BannerTitle,
            model.BannerImage,
            model.BannerUrl,
            model.BannerType,
            model.Sort,
            model.Status,
            model.UpdateBy,
            model.UpdateTime,
            model.Remark
         );

        return res;
    }
}

